package com.ruoyi.web.controller.cp;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.base.Result;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.cp.domain.VipLink;
import com.ruoyi.cp.service.IVipLinkService;
import com.ruoyi.framework.util.FileUploadUtils;
import com.ruoyi.framework.web.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 开通会员线下联系方式 控制层
 * 
 * @author wh
 * @date 2019-06-25
 */
@Slf4j
@Controller
@RequestMapping("/cp/vipLink")
public class VipLinkController extends BaseController {
    private String prefix = "cp/vipLink";
	
	private final IVipLinkService vipLinkService;

	@Autowired
	public VipLinkController(IVipLinkService vipLinkService) {
		this.vipLinkService = vipLinkService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("cp:vipLink:view")
	@GetMapping
	public String vipLink(){
	    return prefix + "/vipLink";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap){
		VipLink vipLink = vipLinkService.getById(id);
		mmap.put("vipLink", vipLink);
		return prefix + "/edit";
	}
	
	/**
	 * 查询开通会员线下联系方式列表
	 */
	@RequiresPermissions("cp:vipLink:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(VipLink vipLink){
		startPage();
        List<VipLink> list = vipLinkService.list(vipLink);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存开通会员线下联系方式
	 */
	@RequiresPermissions("cp:vipLink:add")
	@Log(title = "开通会员线下联系方式", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(VipLink vipLink){
		vipLinkService.save(vipLink);
		return Result.success();
	}

	/**
	 * 修改保存开通会员线下联系方式
	 */
	@RequiresPermissions("cp:vipLink:edit")
	@Log(title = "开通会员线下联系方式", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(VipLink vipLink){
		vipLinkService.updateById(vipLink);
		return Result.success();
	}
	
	/**
	 * 删除开通会员线下联系方式
	 */
	@RequiresPermissions("cp:vipLink:remove")
	@Log(title = "开通会员线下联系方式", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		vipLinkService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出开通会员线下联系方式列表
	 */
	@RequiresPermissions("cp:vipLink:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(VipLink vipLink){
		List<VipLink> list = vipLinkService.list(vipLink);
		ExcelUtil<VipLink> util = new ExcelUtil<>(VipLink.class);
		util.exportExcel(list, "vipLink");
		return Result.success();
	}

	@GetMapping("/qrcode/{vipLinkId}")
	public String qrcode(@PathVariable("vipLinkId") String viplinkId, ModelMap mmap) {
		VipLink vipLink = vipLinkService.getById(viplinkId);
		mmap.put("vipLink", vipLink);
		return prefix + "/qrcode";
	}


	/**
	 * 保存二维码
	 */
	@Log(title = "个人信息", businessType = BusinessType.UPDATE)
	@PostMapping("/updateQrcode")
	@ResponseBody
	public AjaxResult updateQrcode(@RequestParam("qrcodefile") MultipartFile file,
								   @RequestParam("linkId") String linkId) {
		try {
			if (!file.isEmpty()) {
				String qrcode = FileUploadUtils.upload(Global.getQrcodePath(), file);
				VipLink vipLink = vipLinkService.getById(linkId);
				vipLink.setQrcode(qrcode);
				vipLinkService.updateById(vipLink);
				return success();
			}
			 return error();
		} catch (Exception e) {
			log.error("修改头像失败！", e);
			return error(e.getMessage());
		}
	}
}
